﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using System.Data.SqlClient;
using System.Data.OracleClient;

namespace AnLianSysClient
{
    public partial class frmdbset : DevExpress.XtraEditors.XtraForm
    {
        //Public.Common com = new Public.Common();
        public frmdbset()
        {
            InitializeComponent();
            btnSave.Enabled = false; 
        }

        private void dbsetfrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //保存服务器信息
            saveconfiginfo();            
        }

        /// <summary>
        /// 保存界面配置信息到config
        /// </summary>
        private void saveconfiginfo()
        {
            //hx
            //UFIDA.U8C.AnLianDB.common.setConfigInfo("hxsever", edthxserver.Text);
            //UFIDA.U8C.AnLianDB.common.setConfigInfo("hxuser", edthxuser.Text);
            //UFIDA.U8C.AnLianDB.common.setConfigInfo("hxpsd", edthxpsd.Text);
            //UFIDA.U8C.AnLianDB.common.setConfigInfo("hxsid", edthxsid.Text);
            //u8c
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8cserver", edtu8cserver.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8cuser", edtu8cuser.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8cpsd", edtu8cpsd.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8csid", edtu8csid.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8curl", edtu8curl.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8copera", edtopera.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8coppsd", edtoppsd.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("u8cgluser", edtu8cgluser.Text);
            //bank
            UFIDA.U8C.AnLianDB.common.setConfigInfo("bankserver", edtbankserver.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("bankuser", edtbankuser.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("bankpsd", edtbankpsd.Text);
            UFIDA.U8C.AnLianDB.common.setConfigInfo("banksid", edtbanksid.Text); 

        }


        private void dbsetfrm_Load(object sender, EventArgs e)
        {
            getconfiginfo();
        }
          

        private void getconfiginfo()
        {
            //bank
            edtbankserver.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("bankserver");
            edtbankuser.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("bankuser");
            edtbankpsd.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("bankpsd");
            edtbanksid.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("banksid");
            //u8c
            edtu8csid.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8csid");
            edtu8cuser.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8cuser");
            edtu8cpsd.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8cpsd");
            edtu8curl.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8curl");
            edtu8cserver.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8cserver");

            edtopera.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8copera");
            edtoppsd.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8coppsd");
            edtu8cgluser.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("u8cgluser");
            //hx
            //edthxserver.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("hxserver");
            //edthxuser.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("hxuser");
            //edthxpsd.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("hxpsd");
            //edthxsid.Text = UFIDA.U8C.AnLianDB.common.getConfigInfo("hxsid"); 
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            checkconn();            
        }

        private void checkconn()
        {
            string connstring = getconnstring();
            SqlConnection conn = new SqlConnection(connstring);
            
            try
            {
                try
                {
                    conn.Open();
                }
                catch (SqlException)
                {
                    conn.Close();
                    MessageBox.Show("连接失败");
                    return;
                }
            }
            finally
            {
            }
            conn.Close();
            
            MessageBox.Show("连接成功");
        }

        private void checkmysqlconn()
        {
            //string connstring = gethxcconnstring();
            //MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connstring);

            //try
            //{
            //    try
            //    {
            //        conn.Open();
            //    }
            //    catch (MySql.Data.MySqlClient.MySqlException)
            //    {
            //        conn.Close();
            //        MessageBox.Show("连接失败");
            //        return;
            //    }
            //}
            //finally
            //{
            //}
            //conn.Close();

            //MessageBox.Show("连接成功");
        }

        private string getconnstring()
        {
            string connectionString = "Server = " + this.edtbankserver.Text + ";User = " + this.edtbankuser.Text + "; Pwd = " + this.edtbankpsd.Text + ";Database = " + edtbanksid.Text ;
            return connectionString;
        }
        private string getu8cconnstring()
        {
            //Data Source = 10.10.240.56;Initial Catalog = nccdb;User Id = sa;Password = Abc_12345;
            //return "User Id=" + this.edtu8cuser.Text + ";Password=" + this.edtu8cpsd.Text + ";Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + this.edtu8cserver.Text + ")(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=" + this.edtu8csid.Text + ")))";
            return "Server = " + this.edtu8cserver.Text + ";User = " + this.edtu8cuser.Text + "; Pwd = " + this.edtu8cpsd.Text + ";Database = " + edtu8csid.Text;
            //return connectionString;
        }
        //private string gethxcconnstring()
        //{
        //    //mysql "Server = " + getConfigInfo("hxserver") + ";User = " + getConfigInfo("hxuser") + "; password = " + getConfigInfo("hxpsd") + ";Database = " + getConfigInfo("hxsid") +";Charset=utf8";
        //    string connectionString = "Server = " + this.edthxserver.Text + ";User = " + this.edthxuser.Text  + "; password = " + this.edthxpsd.Text + ";Database = " + this.edthxsid.Text + ";Charset=utf8";
        //    return connectionString;
        //}

        private void btnNcTest_Click(object sender, EventArgs e)
        {
            //
            checkoraconn(); 
        }

        private void checkoraconn()
        {
            string connstring = getu8cconnstring();
            //Oracle.ManagedDataAccess.Client.OracleConnection conn = new Oracle.ManagedDataAccess.Client.OracleConnection(connstring);
            SqlConnection conn = new SqlConnection(connstring);

            try
            {
                conn.Open();
            }
            catch (SqlException)
            {
                conn.Close();
                MessageBox.Show("连接失败");
                return;
            }            
            finally
            {
                conn.Close();
            }

            MessageBox.Show("连接成功");
        }

        private void BtnTestHx_Click(object sender, EventArgs e)
        {
            checkmysqlconn();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {

        }

        private void BtnModify_Click(object sender, EventArgs e)
        {

        }
    }
}
